package com.yupi.yuaiagent.demo.invoke;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.messages.AssistantMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * Spring AI 框架调用 AI 大模型
 **/

@Component
 public class SpringAiAiInvoke implements CommandLineRunner{

    @Resource
    private ChatModel dashscopeChatModel;
    @Override
    public void run(String... args) throws Exception {
        Prompt prompt = new Prompt(new UserMessage("你好，我是iran"));

        Flux<ChatResponse> responseFlux = dashscopeChatModel.stream(prompt);

        responseFlux.subscribe(
                chatResponse -> {
                    String content = chatResponse.getResult().getOutput().getText();
                    System.out.print(content);
                },
                error -> System.err.println("调用错误: " + error.getMessage()),
                () -> System.out.println("\n响应完成")
        );

        Thread.sleep(5000);
    }
}
